package com.yuuzy.view;

import com.yuuzy.dao.FundDAO;
import com.yuuzy.model.Fund;
import com.yuuzy.utils.StringUtils;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class InsertFund extends JInternalFrame {
    private final ButtonGroup group = new ButtonGroup();
    private JTextField fund_nameText;
    private JTextField fund_typeText;
    private JTextField fund_risklevelText;
    private JTextField fund_managerText;
    private JTextField fund_amountText;
    private JRadioButton fund_upRB;
    private JRadioButton fund_downRB;
    private FundDAO fundDAO = new FundDAO();


    /**
     * Create the frame.
     */
    public InsertFund() {
        setClosable(true);
        setIconifiable(true);
        setTitle("添加基金产品");
        setBounds(100, 100, 763, 736);

        JLabel lblNewLabel = new JLabel("基金名称：");

        JLabel lblNewLabel_1 = new JLabel("基金类型：");

        JLabel lblNewLabel_2 = new JLabel("风险等级：");

        JLabel lblNewLabel_3 = new JLabel("基金管理者：");

        JLabel lblNewLabel_4 = new JLabel("基金价格：");

        JLabel lblNewLabel_5 = new JLabel("是否上线：");

        fund_nameText = new JTextField();
        fund_nameText.setColumns(10);

        fund_typeText = new JTextField();
        fund_typeText.setColumns(10);

        fund_risklevelText = new JTextField();
        fund_risklevelText.setColumns(10);

        fund_managerText = new JTextField();
        fund_managerText.setColumns(10);

        fund_amountText = new JTextField();
        fund_amountText.setColumns(10);


        JButton btnNewButton = new JButton(" 添加 ");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                insertActionPerformed(e);
            }
        });
        btnNewButton.setIcon(new ImageIcon(InsertFund.class.getResource("/image/addition.png")));

        JButton btnNewButton_1 = new JButton(" 重置 ");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                resetValueActionPerformed(e);
            }
        });
        btnNewButton_1.setIcon(new ImageIcon(InsertFund.class.getResource("/image/reset.png")));

        fund_upRB = new JRadioButton("是");
        group.add(fund_upRB);
        fund_upRB.setSelected(true);

        fund_downRB = new JRadioButton("否");
        group.add(fund_downRB);

        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(157)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addComponent(lblNewLabel)
                                                        .addComponent(lblNewLabel_1)
                                                        .addComponent(lblNewLabel_2)
                                                        .addComponent(lblNewLabel_3)
                                                        .addComponent(lblNewLabel_4)
                                                        .addComponent(lblNewLabel_5))
                                                .addGap(63)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addComponent(fund_upRB)
                                                                .addGap(126)
                                                                .addComponent(fund_downRB, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE))
                                                        .addComponent(fund_nameText, GroupLayout.PREFERRED_SIZE, 316, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(fund_typeText, GroupLayout.PREFERRED_SIZE, 316, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(fund_risklevelText, GroupLayout.PREFERRED_SIZE, 316, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(fund_managerText, GroupLayout.PREFERRED_SIZE, 316, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(fund_amountText, GroupLayout.PREFERRED_SIZE, 316, GroupLayout.PREFERRED_SIZE)))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(209)
                                                .addComponent(btnNewButton)
                                                .addGap(121)
                                                .addComponent(btnNewButton_1)))
                                .addContainerGap(103, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(82)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel)
                                        .addComponent(fund_nameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(44)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_1)
                                        .addComponent(fund_typeText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(52)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_2)
                                        .addComponent(fund_risklevelText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(52)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_3)
                                        .addComponent(fund_managerText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(45)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_4)
                                        .addComponent(fund_amountText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(53)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_5)
                                        .addComponent(fund_upRB)
                                        .addComponent(fund_downRB))
                                .addGap(64)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(btnNewButton)
                                        .addComponent(btnNewButton_1))
                                .addContainerGap(110, Short.MAX_VALUE))
        );
        getContentPane().setLayout(groupLayout);

    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    InsertFund frame = new InsertFund();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 添加事件处理
     *
     * @param evt
     */
    private void insertActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        String fund_name = this.fund_nameText.getText();
        String fund_type = this.fund_typeText.getText();
        String fund_risklevel = this.fund_risklevelText.getText();
        String fund_manager = this.fund_managerText.getText();
        String fund_amount = this.fund_amountText.getText();

        if (StringUtils.isEmpty(fund_name)) {
            JOptionPane.showMessageDialog(null, "基金名称不能为空！");
            return;
        }

        if (StringUtils.isEmpty(fund_type)) {
            JOptionPane.showMessageDialog(null, "基金类型不能为空！");
            return;
        }

        if (StringUtils.isEmpty(fund_risklevel)) {
            JOptionPane.showMessageDialog(null, "风险等级不能为空！");
            return;
        }

        if (StringUtils.isEmpty(fund_manager)) {
            JOptionPane.showMessageDialog(null, "基金管理者不能为空！");
            return;
        }

        if (StringUtils.isEmpty(fund_amount)) {
            JOptionPane.showMessageDialog(null, "基金价格不能为空！");
            return;
        }

        String fund_up = "";
        if (fund_upRB.isSelected()) {
            fund_up = "1";
        } else if (fund_downRB.isSelected()) {
            fund_up = "0";
        }


        if (StringUtils.isEmpty(fund_up)) {
            JOptionPane.showMessageDialog(null, "请选择是否上线该基金");
            return;
        }

        Fund fund = new Fund(fund_name, fund_type, fund_risklevel, fund_manager, StringUtils.changeDouble(fund_amount), fund_up);
        int insertNum = fundDAO.insertFund(fund);
        if (insertNum > 0) {
            JOptionPane.showMessageDialog(null, "基金添加成功！");
            resetValue();
        } else {
            JOptionPane.showMessageDialog(null, "基金添加失败！");
        }
    }


    /**
     * 重置事件处理
     *
     * @param evt
     */
    private void resetValueActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        this.resetValue();
    }

    /**
     * 重置表单
     */
    private void resetValue() {
        this.fund_nameText.setText("");
        this.fund_typeText.setText("");
        this.fund_risklevelText.setText("");
        this.fund_managerText.setText("");
        this.fund_amountText.setText("");
        this.group.clearSelection();
    }
}
